/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "GameProject.h"

#include "FileSystem.h"
#include "DataSerializer.h"

#include <QtCore/QString>
#include <QtCore/QDir>

namespace PQEngine
{
	const std::string GameProject::PERSIS_FILE_NAME="Project.gdt";
	GameProject::GameProjectMap GameProject::_map;


	GameProject::GameProject( std::string path )
		: _path(path)
	{
		_name=FileSystem::getPathLastNode(path);

		QString name=QString::fromStdString(_name);
		//create directory contain all project data
		//FileSystemSingleton.createDirectory(_path);

		std::string parentPath=FileSystemSingleton.getParentPath(path);
		QString parent=QString::fromStdString(parentPath);
		QDir dir(parent);
		if(dir.exists()){
			if(!dir.exists(name)){
				dir.mkdir(name);
			}

		}
		

		//
		//loadAssetsArchive();
	}

	void GameProject::loadAssetsArchive()
	{
		GPKArchiveManagerSingleton.init(_path);
	}

	void GameProject::persist()
	{
		//A project contain the asserts archives,should persist their names.
		
	}

	GameProject* GameProject::get(std::string path)
	{
		GameProjectMap::iterator it= _map.find(path);
		if(it!=_map.end()){
			return it->second;
		}

		
		GameProject* project=new GameProject(path);
		_map.insert(GameProjectMap::value_type(path,project));

		return project;
	}

	GPKArchive* GameProject::createAssetArchive(std::string name)
	{
		return GPKArchiveManagerSingleton.createArchive(name);
	}
}
